package com.test.mytest.saolei.my;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class SelectDialog extends Dialog {

	protected Shell shell;
	private Text text;
	private Text text_1;
	private Text text_2;
	
	private int[] ints=new int[3];
	private Button btnRadioButton;
	private Button btnx;
	private Button btnx_1;
	private Button btnx_2;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public SelectDialog(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public int[] open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return ints;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(450, 300);
		shell.setText(getText());
		
		Group group = new Group(shell, SWT.NONE);
		group.setText("难度");
		group.setBounds(10, 10, 89, 135);
		
		btnx = new Button(group, SWT.RADIO);
		btnx.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				System.setProperty("selectType", ""+1);
				btnRadioButton.setSelection(false);
				ints[0]=5;
				ints[1]=5;
				ints[2]=10;
			}
		});
		btnx.setBounds(10, 27, 97, 17);
		btnx.setText("5x5");
		
		btnx_1 = new Button(group, SWT.RADIO);
		btnx_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				System.setProperty("selectType", ""+2);
				btnRadioButton.setSelection(false);
				ints[0]=9;
				ints[1]=9;
				ints[2]=25;
			}
		});
		btnx_1.setBounds(10, 62, 97, 17);
		btnx_1.setText("9x9");
		
		btnx_2 = new Button(group, SWT.RADIO);
		btnx_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				System.setProperty("selectType", ""+3);
				btnRadioButton.setSelection(false);
				ints[0]=16;
				ints[1]=16;
				ints[2]=60;
			}
		});
		btnx_2.setBounds(10, 85, 97, 17);
		btnx_2.setText("16x16");
		
		Group group_1 = new Group(shell, SWT.NONE);
		group_1.setBounds(120, 33, 117, 112);
		
		Label label = new Label(group_1, SWT.NONE);
		label.setBounds(10, 21, 22, 17);
		label.setText("长");
		
		text = new Text(group_1, SWT.BORDER);
		text.setBounds(34, 21, 73, 23);
		
		Label label_1 = new Label(group_1, SWT.NONE);
		label_1.setBounds(10, 50, 22, 17);
		label_1.setText("高");
		
		text_1 = new Text(group_1, SWT.BORDER);
		text_1.setBounds(34, 50, 73, 23);
		
		Label label_2 = new Label(group_1, SWT.NONE);
		label_2.setBounds(10, 85, 29, 17);
		label_2.setText("数量");
		
		text_2 = new Text(group_1, SWT.BORDER);
		text_2.setBounds(44, 79, 73, 23);
		
		btnRadioButton = new Button(shell, SWT.RADIO);
		btnRadioButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				System.setProperty("selectType", ""+4);
				btnx.setSelection(false);
				btnx_1.setSelection(false);
				btnx_2.setSelection(false);
			}
		});
		btnRadioButton.setBounds(120, 10, 97, 17);
		btnRadioButton.setText("自定义");
		
		Button button = new Button(shell, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (btnRadioButton.getSelection()) {
					ints[0]=Integer.valueOf(text.getText());
					ints[1]=Integer.valueOf(text_1.getText());
					ints[2]=Integer.valueOf(text_2.getText());
					System.setProperty("width", text.getText());
					System.setProperty("heith", text_1.getText());
					System.setProperty("num", text_2.getText());
				}
				shell.dispose();
			}
		});
		button.setBounds(125, 191, 80, 27);
		button.setText("开始");
		
		Button button_1 = new Button(shell, SWT.NONE);
		button_1.setBounds(211, 191, 80, 27);
		button_1.setText("取消");
		String property = System.getProperty("selectType");
		if (property != null) {
			int type = Integer.valueOf(property);
			switch (type) {
			case 1:
				setSelection(true,false,false,false);
				break;
			case 2:
				setSelection(false,true,false,false);
				break;
			case 3:
				setSelection(false,false,true,false);
				break;
			case 4:
				setSelection(false,false,false,true);
				text.setText(System.getProperty("width"));
				text_1.setText(System.getProperty("heith"));
				text_2.setText(System.getProperty("num"));
				break;
			}
		}
		
	}

	private void setSelection(boolean b, boolean c, boolean d, boolean e) {
		btnx.setSelection(b);
		btnx_1.setSelection(c);
		btnx_2.setSelection(d);
		btnRadioButton.setSelection(e);
	}
}
